package com.sicpa.web.controller.common;

import com.sicpa.common.core.domain.AjaxResult;
import com.sicpa.common.core.domain.entity.AntiCounterfeitingUnifiedFile;
import com.sicpa.common.utils.file.FileUtils;
import com.sicpa.system.service.IAntiCounterfeitingUnifiedFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * FtpController
 *
 * @author lwf
 */
@RestController
@RequestMapping("/ftp")
public class FtpController {

    private IAntiCounterfeitingUnifiedFileService iAntiCounterfeitingUnifiedFileService;

    @Autowired
    public FtpController(IAntiCounterfeitingUnifiedFileService iAntiCounterfeitingUnifiedFileService) {
        this.iAntiCounterfeitingUnifiedFileService = iAntiCounterfeitingUnifiedFileService;
    }

    /**
     * 统一ftp上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws IOException, InterruptedException {
        AntiCounterfeitingUnifiedFile antiCounterfeitingUnifiedFile = iAntiCounterfeitingUnifiedFileService.add(file);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("fileName", antiCounterfeitingUnifiedFile.getFileName());
        ajax.put("fileId", antiCounterfeitingUnifiedFile.getFileId());
        return ajax;
    }


    /**
     * 统一ftp下载pdf文件
     * @param fileId
     * @return
     * @throws IOException
     */
    @GetMapping("/contactDownload")
    public void contactDownloadFile(String fileId, String fileName,HttpServletResponse response, HttpServletRequest request) throws IOException, InterruptedException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("referer","shcpa.org.cn");
        fileName = fileName+"的申请修改操作人附件.pdf";
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, fileName));
        iAntiCounterfeitingUnifiedFileService.download(fileId,fileName,response);
    }

    /**
     * 统一ftp下载文件
     * @param fileId
     * @return
     * @throws IOException
     */
    @GetMapping("/fileDownload")
    public void downloadFile(String fileId, String fileName,HttpServletResponse response, HttpServletRequest request) throws IOException, InterruptedException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("referer","shcpa.org.cn");
      //  fileName = fileName+".pdf";
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, fileName));
        iAntiCounterfeitingUnifiedFileService.downloadBigFile(fileId,fileName,response);
    }
}
